// JavaScript Document
var MOUSE_L_DOWN = 0;
var MOUSE_R_DOWN = 6;
var MOUSE_UP = 1;
var MOUSE_MOVE = 2;
var MOUSE_DBCLICK = 4;
var MOUSE_CLICK = 3;
var MOUSE_ONCONTEXTMENU = 5;

var KEY_ESC = 27;
var KEY_DELETE = 46;
var KEY_A = 65;
var KEY_B = 66;
var KEY_C = 67;
var KEY_D = 68;
var KEY_E = 69;
var KEY_F = 70;
var KEY_G = 71;
var KEY_H = 72;
var KEY_I = 73;
var KEY_J = 74;
var KEY_K = 75;
var KEY_L = 76;
var KEY_M = 77;
var KEY_N = 78;
var KEY_O = 79;
var KEY_P = 80;
var KEY_Q = 81;
var KEY_R = 82;
var KEY_S = 83;
var KEY_T = 84;
var KEY_U = 85;
var KEY_V = 86;
var KEY_W = 87;
var KEY_X = 88;
var KEY_Y = 89;
var KEY_Z = 90;

var KEY_TAB = 9;
var KEY_SHIFT = 16;
var KEY_CTRL = 17;
var KEY_CTRL = 18;
var KEY_SPACE = 32;

var KEY_LEFT = 37;
var KEY_UP = 38;
var KEY_RIGHT = 39;
var KEY_DOWN = 40;
var KEY_ENTER = 13;

var KEY_F1 = 112;
var KEY_F2 = 113;
var KEY_F3 = 114;
var KEY_F4 = 115;
var KEY_F5 = 116;
var KEY_F6 = 117;
var KEY_F7 = 118;
var KEY_F8 = 119;
var KEY_F9 = 120;
var KEY_F10 = 121;
var KEY_F11 = 122;
var KEY_F12 = 123;

var PERIOD = 190; //@Robot '.号的keyCode
var ARROW_DOWN = 40; //@Robot arrow down 的keyCode
var ARROW_UP = 38; //@Robot arrow down 的keyCode
var ARROW_LEFT = 37; //@Robot arrow up 的keyCode
var ARROW_RIGHT= 39; //@Robot arrow up 的keyCode
var DISPLAY_NUM = 5;//@Robot 提示框显示个数
var ENTER = 13;//@Robot Enter 的keyCode
var BACKSPACE = 8;//@Robot backspace 的keyCode
var NUM_PERIOD = 110;//@Robot 数字'.'按键keyCode
var FORWARD_SLASH = 191;//@Robot '/'按键keyCode
var KEY_ZERO = 96;//@Robot '0'按键keyCode
var KEY_NINE = 105;//@Robot '9'按键keyCode
var specialKeyArray = ['`','~','!','@','#','$','%','^','&','*','(',')','-','=','+','{','}',':',';',',','>','<','?','/','|','\\',' ','→','←'];

function CanvasEventObj(eventType, loc, canvas) {
	this.eventType = eventType;
	this.location = loc;
	this.canvas = canvas;
}

function canvasViewerEventInit(canvas) {
	var startLoc = null;
	canvas.entity.draggable = true;
	// move
	canvas.entity.onmousemove = function(e) {
		var loc = getLocation(e, canvas);
		var eventObj = new CanvasEventObj(MOUSE_MOVE, loc, canvas);
		// {eventType:MOUSE_MOVE,location:loc};
		canvas.fire(eventObj);
	};

	// click
	canvas.entity.onclick = function(e) {
		var eventObj = new CanvasEventObj(MOUSE_CLICK, startLoc, canvas);
		// {eventType:MOUSE_DBCLICK,location:startLoc};
		canvas.fire(eventObj);
	};

	// DB click
	canvas.entity.ondblclick = function(e) {
		for(var i in this){
			console.log(i+':'+this[i]);
		}
	};

	// down
	canvas.entity.onmousedown = function(e) {
		e.preventDefault();
		startLoc = getLocation(e, canvas);
		// console.log('canvas{x:' + startLoc.x + ',y:' + startLoc.y + '}');
		var status = MOUSE_L_DOWN;
		if (e.button == 2) {
			status = MOUSE_R_DOWN;
		}
		var eventObj = new CanvasEventObj(status, startLoc, canvas);
		_dss_editor.activeCanvas = canvas.presenterName;
		canvas.fire(eventObj);
	};

	// up
	canvas.entity.onmouseup = function(e) {
		var loc = getLocation(e, canvas);
		var offsetLoc = {
			x : 0,
			y : 0
		};
		offsetLoc.x = loc.x - startLoc.x;
		offsetLoc.y = loc.y - startLoc.y;
		var eventObj = new CanvasEventObj(MOUSE_UP, offsetLoc, canvas);
		canvas.fire(eventObj);
	};

	//oncontext
	canvas.entity.oncontextmenu = function(e) {
		e.preventDefault();
		startLoc = getLocation(e, canvas);
		var eventObj = new CanvasEventObj(MOUSE_ONCONTEXTMENU, startLoc, canvas);
		canvas.fire(eventObj);
	};

	canvas.entity.onDrag = function(e) {
		console.log('drag');
	};
	//scorll
	canvas.entity.parentNode.onscroll = function(e) {
		var input = canvas.input;
		//让输入框跟着一起走
		if (input) {
			input.layout();
		}
		_dss_editor._model.system.refreshSystemSelectionHandle(canvas);
		_dss_editor._model.link.refreshLinkSelectionHandle(canvas);
		canvas.update();
		//			canvas.matchSize();
	};

};

function initDocEvent() {
	var startLoc = null;
	document.onmousedown = function(e) {		
		var canvas = _dss_editor.canvas[_dss_editor.activeCanvas];
		if(canvas != null){
			//e.preventDefault();
			startLoc = getLocation(e, canvas);
		}
		
		// console.log('doc{x:' + startLoc.x + ',y:' + startLoc.y + '}');
	};
	//key event
	document.onkeydown = function(e) {
		
		var canvas = _dss_editor.canvas[_dss_editor.activeCanvas];
		// console.log(canvas);
		if (canvas != null && !canvas.input) {
			// e.preventDefault();
		} else {
			return;
		}
		var eventObj = {};
		eventObj.location = startLoc;
		eventObj.canvas = canvas;
		if (e.ctrlKey) {
			eventObj.ctrl = true;
		}
		eventObj.eventType = e.keyCode;
		var done = canvas.fire(eventObj);
		if(done){
			e.preventDefault();
		}
			
		return true;
	};
}

paletteViewer.Event = {
	initItem : function(node) {
		var item = {};
		if (node.hasOwnProperty('text')) {
			item.name = node.text;
		}
		if (node.hasOwnProperty('attributes')) {
			if (node.attributes.hasOwnProperty('type')) {
				item.type = node.attributes.type;
			}
			if (node.attributes.hasOwnProperty('baseOn')) {
				item.baseOn = node.attributes.baseOn;
			}
		}
		return item;
	},

	init : function() {
		$('#' + PALETTE_SYSTEM).tree({
			onClick : function(node) {
				var item = paletteViewer.Event.initItem(node);
				// item.name = node.text;
				// item.type = node.attributes.type;
				//				item.type_id = node.attributes.type_id;
				paletteViewer.selectionItem = item;
				restoreOtherFolder(PALETTE_SYSTEM);
			},
			onLoadError : function(error) {
				$('#' + PALETTE_SYSTEM).tree('reload');
			}
		});
		$('#' + PALETTE_LINK).tree({
			onClick : function(node) {
				var item = paletteViewer.Event.initItem(node);
				// item.name = node.text;
				// item.type = node.attributes.type;
				//				item.type_id = node.attributes.type_id;
				paletteViewer.selectionItem = item;
				restoreOtherFolder(PALETTE_LINK);
			},
			onLoadError : function(error) {
				$('#' + PALETTE_LINK).tree('reload');
			}
		});

		$('#' + PALETTE_OTHER).tree({
			onClick : function(node) {
				var item = paletteViewer.Event.initItem(node);
				// item.name = node.text;
				// item.type = node.attributes.type;
				paletteViewer.selectionItem = item;
				restoreOtherFolder(PALETTE_OTHER);
			},
			onLoadError : function(error) {
				$('#' + PALETTE_OTHER).tree('reload');
			}
		});
		$('#' + PALETTE_TOOL).tree({
			onClick : function(node) {
				paletteViewer.selectionItem = node.text;
				restoreOtherFolder(PALETTE_TOOL);
				//				canvas.entity.style.cursor = 'default';
				//				canvas.update();
			},
			onLoadError : function(error) {
				$('#' + PALETTE_TOOL).tree('reload');
			}
		});
		$('#' + PALETTE_MISC).tree({
			onClick : function(node) {
				var item = paletteViewer.Event.initItem(node);
				// item.name = node.text;
				// item.type = node.attributes.type;
				//				item.type_id = node.attributes.type_id;
				paletteViewer.selectionItem = item;
				restoreOtherFolder(PALETTE_MISC);
			},
			onLoadError : function(error) {
				$('#' + PALETTE_MISC).tree('reload');
			}
		});
	}
};

function getLocation(e, canvas) {
	var location = _dss_editor._toolkit.getOffsetLocation(canvas);
	location.x = (e.pageX + location.x);
	location.y = (e.pageY + location.y);
	return location;
}

